15 virt-install examples KVM virtualization commands cheatsheet
Sommaire
4. virt-install --os-type and --os-variant
6. virt-install example with multiple networks
8. virt-install console ttyS0 (--extra-args)
9. virt-install --noautoconsole
10. virt-install --graphics with virt-viewer
11. virt-install --graphics with VNC Viewer
12. virt-install --disk (path, size, format)
14. virt-install example with multiple disks
15. virt-install --pxe and --boot network
In this article I will share a list of commands to create KVM Virtual machine command line using virt-install examples in RHEL/CentOS 8 Linux.
virt-install is a command line tool for creating new KVM, Xen, or Linux container guests using the libvirt hypervisor management library
The virt-install tool provides a number of options that can be passed on the command line. To see a complete list of options run the following command:
# virt-install --help
ALSO READ:
You can also create KVM Virtual Machines using
⇒ Cockpit Web Console GUI
⇒ Virtual Manager (Deprecated starting RHEL/CentOS 8)
As a pre-requisite, you must first install KVM and enable KVM Virtualization on your Linux server host before you can create KVM Virtual Machine using virt-install or any other method in RHEL/CentOS 8 Linux
To define a name of the VM use --name with virt-install command
[root@rhel-8 ~]# virt-install --name centos8-2
To define CPU for the VM use --vcpus with virt-install command
[root@rhel-8 ~]# virt-install --name centos8-2 --vcpus 2
To define memory for the VM use --memory with virt-install command
[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240
You can define OS Variant and OS Type when you create VM with virt-install command
Next use --os-variant and --os-type to define the OS Variant and OS Type respectively
To get the list of supported OS Variant types, execute below command from the terminal
# osinfo-query os
In the below virt-install example cheat sheet I have defined --os-type as Linux and --os-variant as Centos7.0
[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --os-type=Linux --os-variant=centos7.0
To check list of available networks use virsh command
[root@rhel-8 ~]# virsh net-list
Name State Autostart Persistent
----------------------------------------------------------
default active yes yes
In the below virt-install example cheat sheet we will create KVM VM and assign this default network for our Virtual Machine
[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --os-type=Linux --os-variant=centos7.0 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso --network network=default --graphics=vnc -v
Using centos7.0 default --disk size=10
Starting install...
Retrieving file vmlinuz... | 6.3 MB 00:00:00
Retrieving file initrd.img... | 52 MB 00:00:00
Allocating 'centos8-2-3.qcow2' | 10 GB 00:00:00
You can use --network argument multiple times to add multiple network cards to the VM
In this virt-install example cheat sheet I am creating KVM VM with one network using bridge connection and other network using default network
[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --os-type=Linux --os-variant=centos7.0 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso --network network=default --network bridge=nm-bridge --console pty,target_type=serial --graphics=vnc -v
HINT:
You can either use --location or --cdrom to initiate the installation. I observed one difference with --location and --cdrom, with --cdrom the VM just boots using the provided ISO while with --location the installation starts automatically
•.Below virt-install example uses --location argument to define the path of the ISO which will be used to create KVM Virtual Machine.
•.In this command we have not defined any --graphics
•.So the problem with this command would be that you will connect to the new VM using serial console but since console is not defined you will end up with no STDOUT
•.In such case your virt-install will be stuck at "Escape character is ^]" prompt
[root@rhel-8 ~]# virt-install --name centos8-20 --memory 10240 --vcpus=2 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso --network bridge=nm-bridge --graphics=none -v
Using rhel7.6 default --disk size=10
Starting install...
Retrieving file vmlinuz... | 6.3 MB 00:00:00
Retrieving file initrd.img... | 52 MB 00:00:00
Allocating 'centos8-20.qcow2' | 10 GB 00:00:00
Connected to domain centos8-20
Escape character is ^]
Also if you try to connect to your VM using virsh or Virtual Manager, you will get the same output
[root@rhel-8 ~]# virsh console centos8-2
Connected to domain centos8-2
Escape character is ^]
error: operation failed: Active console session exists for this domain
Hence this combination is not recommended to create KVM Virtual Machine
•.Now in the earlier virt-install example, we used --location to define the ISO with --graphics=none
•.Due to this combination we ended up with a serial console connection with no graphical access
•.You can combine --location with --extra-args to define additional kernel arguments which will be considered for installation
•.Use --extra-args console=ttyS0 to redirect the STDOUT on your terminal window
•.The problem with this command would be that you will again connect to the new VM using serial console
•.So if you have to select custom options to perform the installation, using serial console it may be difficult for you
•.For normal use case where we need graphical console to perform the installation, do not use this command
[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso --network bridge=nm-bridge --graphics=none --extra-args console=ttyS0 -v
Using centos7.0 default --disk size=10
Starting install...
Retrieving file vmlinuz... | 6.3 MB 00:00:00
Retrieving file initrd.img... | 52 MB 00:00:00
Allocating 'centos8-2.qcow2' | 10 GB 00:00:00
Connected to domain centos8-2
Escape character is ^]
[ 0.000000] Initializing cgroup subsys cpuset
[ 0.000000] Initializing cgroup subsys cpu
[ 0.000000] Initializing cgroup subsys cpuacct
use Ctrl+5 to exit the session
To reconnect to the session use below command
[root@rhel-8 ~]# virsh console centos8-2
NOTE:
You can also use --cdrom in these commands, but just to highlight, you cannot use --cdrom with kernel arguments --extra-args and the outcome would be same so it is recommended to always use --graphics while creating VM with virt-install.
You can choose to use this command based on your requirement.
•.Don't automatically try to connect to the guest console.
•.By default the virt-install command will continue to wait for the installation to complete after creating the VM
•.If you wish to detach from the virt-install session then you can use --noautoconsole
•.If your command requested a multistep install, like --cdrom or --location, after the install phase is complete the VM will be shutoff, regardless of whether a reboot was requested in the VM.
•.If you want the VM to be rebooted, virt-install must remain running.
•.You can use '--wait' to keep virt-install alive even if --noautoconsole is specified.
[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso --network bridge=nm-bridge --graphics=none --console pty,target_type=serial --noautoconsole --extra-args console=ttyS0 -v
Using centos7.0 default --disk size=10
Starting install...
Retrieving file vmlinuz... | 6.3 MB 00:00:00
Retrieving file initrd.img... | 52 MB 00:00:00
Allocating 'centos8-2-17.qcow2' | 10 GB 00:00:00
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
As expected the command exited after creating the VM. Next connect to the console using virsh
[root@rhel-8 ~]# virsh console centos8-2
•.In the earlier virt-install examples we disabled graphics which is why the end result was not very user friendly
•.In this command we will use virt-viewer to view the console after creating the VM
•.You can install virt-viewer using yum -y install virt-viewer on RHEL/CentOS 7/8 Linux
•.I have defined --graphics to use VNC to perform installation
•.At the end of this command execution, virt-viewer will automatically connect to the new VM's console
•.This is the recommended command for most use cases to create KVM Virtual Machine using virt-install
[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso --network bridge=nm-bridge --graphics=vnc --console pty,target_type=serial -v
Starting install...
Retrieving file vmlinuz... | 6.3 MB 00:00:00
Retrieving file initrd.img... | 52 MB 00:00:00
Allocating 'centos8-2-17.qcow2' | 10 GB 00:00:00
Domain creation completed.
Restarting guest.
Next virt-viewer will automatically connect to the centos8-2 console
•.Similar to virt-viewer you can also connect to your new VM using VNC Viewer
•.In this virt-install example cheat sheet I have executed virt-install from CLI in one terminal with --noautoconsole to detach the session
•.Next I will connect to the VM console using VNC viewer
[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --os-type=Linux --os-variant=centos7.0 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso --network bridge=nm-bridge --graphics type=vnc --console pty,target_type=serial --noautoconsole -v
Using centos7.0 default --disk size=10
Starting install...
Retrieving file vmlinuz... | 6.3 MB 00:00:00
Retrieving file initrd.img... | 52 MB 00:00:00
Allocating 'centos8-2-14.qcow2' | 10 GB 00:00:00
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
To get the VNC port number VNC is listening on use virsh command
[root@rhel-8 ~]# virsh vncdisplay centos8-2
127.0.0.1:3
Install tigervnc on your Linux server
# yum -y install tigervnc
Next execute vncviewer to connect to the console
# vncviewer <address>:<port>
# vncviewer 127.0.0.1:3
•.In all the above virt-install examples we were using default storage path and disk size to create KVM Virtual Machine
•.To define custom path to store the disks use --disk argument
•.To define disk size of your VM use "size" with --disk argument. The provided value will be considered in GB
•.You can use 'raw', 'qcow2', 'vmdk' as supported disk formats
•.In this virt-install example command, the 20GB disk for the VM will be created under /disks as centos8-2.qcow2
[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --cdrom=/tmp/rhel-server-7.6-x86_64-dvd.iso --disk path=/disks/centos8-2.qcow2,size=20,format=qcow2 --network bridge=nm-bridge --graphics=vnc -v
Starting install...
By default the VMs are configured to stay powered off when the host server reboots
You can use --autostart to also start the VM when the host server comes up after reboot
[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --cdrom=/tmp/rhel-server-7.6-x86_64-dvd.iso --disk path=/disks/centos8-2.qcow2,size=20,format=qcow2 --disk path=/disks/centos8-2-1.qcow2,size=20,format=qcow2 --network bridge=nm-bridge --graphics=vnc --autostart -v
•.In the earlier virt-install example we used single disk to create KVM Virtual Machine
•.You can also define multiple disks with virt-install using --disk argument multiple times
•.In this virt-install example I am creating KVM Virtual Machine with 2 disks with a custom path under /disks
[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --cdrom=/tmp/rhel-server-7.6-x86_64-dvd.iso --disk path=/disks/centos8-2.qcow2,size=20,format=qcow2 --disk path=/disks/centos8-2-1.qcow2,size=20,format=qcow2 --network bridge=nm-bridge --graphics=vnc -v
Starting install...
Allocating 'centos8-2.qcow2' | 20 GB 00:00:00
Allocating 'centos8-2-1.qcow2' | 20 GB 00:00:00
Verify the list of disks assigned to this VM
[root@rhel-8 ~]# ls -l /disks/
total 6808
-rw-------. 1 qemu qemu 21478375424 Apr 29 03:10 centos8-2-1.qcow2
-rw-------. 1 qemu qemu 21478375424 Apr 29 03:10 centos8-2.qcow2
•.Create VM using virt-install command to perform installation using PXE
•.So if you have a working PXE server then just use --pxe with virt-install
•.If you also define --location with --pxe, then virt-install will get the initrd and vmlinuz file from the provided --location of the ISO
•.To also specific kickstart file along with PXE use --extra-args=
[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --os-type=Linux --os-variant=centos7.0 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso --network network=default --pxe --extra-args "ks=/tmp/kickstart.conf" --console pty,target_type=serial --graphics=vnc -v
Alternatively you can also use "--boot network" to boot from PXE server
[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --os-type=Linux --os-variant=centos7.0 --network network=default --boot network --console pty,target_type=serial --graphics=vnc -v
I have written another article with the detailed steps to perform PXE installation on KVM Virtual Machines
Lastly I hope this article with virt-install examples cheat sheet to create KVM Virtual Machine on Linux was helpful. So, let me know your suggestions and feedback using the comment section.